package com.demo.myapplication.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import android.widget.SimpleAdapter;

import com.demo.myapplication.R;
import com.demo.myapplication.utils.CommonUtils;
import com.demo.myapplication.utils.MultiChoiceListener;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;


public class ListActionModeFragment extends Fragment {
  private String Tag = ListActionModeFragment.class.getSimpleName();

  private ListView list;

  private ArrayList<Map<String, Object>> dataList;

  private void initData() {
    dataList = new ArrayList<>();
    for (int i = 0; i < CommonUtils.icon.length; i++) {
      Map<String, Object> map = new HashMap<>();
      map.put(CommonUtils.map_icon_key, CommonUtils.icon[i]);
      map.put(CommonUtils.map_text_key, getString(CommonUtils.iconName[i]));
      dataList.add(map);
    }
  }

  @Nullable
  @Override
  public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    View v = inflater.inflate(R.layout.fragment_list_action_mode, container, false);
    list = (ListView) v.findViewById(R.id.list);
    initData();
    setupList();
    return v;
  }

  private void setupList() {
    SimpleAdapter adapter = new SimpleAdapter(getActivity(), dataList, R.layout.list_icon_item,
        new String[]{CommonUtils.map_icon_key, CommonUtils.map_text_key}, new int[]{R.id.icon, R.id.text});
    list.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE_MODAL);
    list.setAdapter(adapter);
    list.setMultiChoiceModeListener(new MultiChoiceListener(list, getActivity()).setDataList(dataList));
  }


  public static Fragment newInstance(Bundle args) {
    ListActionModeFragment fragment = new ListActionModeFragment();
    fragment.setArguments(args);
    return fragment;
  }

}
